package com.eascs.web.o2o.web.reward.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;
import com.eascs.web.o2o.web.common.BaseMasterViewController;
import com.eascs.web.o2o.web.reward.entity.Province;
import com.eascs.web.o2o.web.reward.entity.ProvinceSignForm;
import com.eascs.web.o2o.web.reward.service.ProvinceNameData;
import com.eascs.web.o2o.web.reward.service.SignForProvinceService;
import com.eascs.web.o2o.web.util.BankRowWriteHandler;

@Controller
@RequestMapping(value="/provinceSign")
public class SignForProvinceController extends BaseMasterViewController{
	
	@Autowired
	ProvinceNameData provinceNameData;
	
	@Autowired
	SignForProvinceService signForProvinceService;

	@RequestMapping(value="/index")
	public String showSignForProvince(ProvinceSignForm entity) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "signForProvince");
		String o2oUrl = "/restProvinceSign/provinceSignList";
		if(entity != null) {
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "provinceName", entity.getProvinceName());
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "dateStart", entity.getDateStart());
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "dateEnd", entity.getDateEnd());
			o2oUrl = o2oUrl + "&pageIndex=";
		} else {
			o2oUrl = o2oUrl + "?pageIndex=";
		}
		List<Province> proList = provinceNameData.getProvinceNameData();
		ViewContext.put("vo", entity);
		ViewContext.put("o2oUrl", o2oUrl);
		ViewContext.put("proList", proList);
		return "/evaluate/province_sign";
	}
	
	@RequestMapping(value="/download")
	public void downloadReport(ProvinceSignForm entity, HttpServletResponse httpServletResponse, HttpServletRequest request) {
		entity.setPageSize(Integer.MAX_VALUE);

		PageResult<Object[]> pageResult = signForProvinceService.getProvinceSignList(entity);
		String       path    = SignForProvinceController.class.getResource("/excelTemplate").getPath();
		StringBuffer s       = new StringBuffer();
		String       outPath = s.append(path).append("/province-sign.xls").toString();
		try {
			String outfile = "加签率汇总.xls";
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes     = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists()) throw new RuntimeIOException("file not found");

			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");

			// 开始的表格，行，列
			int sheetIndex  = 0;
			int rowIndex    = 1;
			int columnIndex = 0;
			// 包含的列
			int col = 5;
			// 需要统计的列
			int[]  arr = {};
			
			BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(pageResult.getData(), col, arr, null, sheetIndex, rowIndex, columnIndex);
			ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(), SignForProvinceController.class.getResourceAsStream("/excelTemplate/province-sign.xls"), bankRowWriteHandler);

		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} 
	}
}
